package com.bruceewu.galleryphoto.page;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Switch;
import android.widget.TextView;

import com.bruceewu.galleryphoto.base.BaseActivity;
import com.bruceewu.galleryphoto.R;
import com.bruceewu.galleryphoto.utis.ImageChooser;
import com.bruceewu.galleryphoto.utis.SharedPreferenceHelper;
import com.bruceewu.galleryphoto.utis.ToastUtils;
import com.bruceewu.galleryphoto.utis.UriParser;

public class MainActivity extends BaseActivity {
    private static final int REQUEST_CODE = 100;
    private int TIME_PERIOD = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initLocal();
        setClick(R.id.choose, () -> ImageChooser.showChoose(this, REQUEST_CODE));
        setClick(R.id.photo, () -> PhotoActivity.Open(this, null));
        setClick(R.id.add, () -> {
            TIME_PERIOD++;
            resetTime();
        });
        setClick(R.id.minus, () -> {
            TIME_PERIOD--;
            if (TIME_PERIOD <= 1) {
                TIME_PERIOD = 1;
            }
            resetTime();
        });
        ((Switch) findViewById(R.id.switchBack)).setChecked(SharedPreferenceHelper.getShowBack());
        ((Switch) findViewById(R.id.switchIndicator)).setChecked(SharedPreferenceHelper.getShowIndicator());
        ((Switch) findViewById(R.id.switchTip)).setChecked(SharedPreferenceHelper.getShowTip());
        ((Switch) findViewById(R.id.switchBack)).setOnCheckedChangeListener((buttonView, isChecked) -> SharedPreferenceHelper.putShowBack(isChecked));
        ((Switch) findViewById(R.id.switchIndicator)).setOnCheckedChangeListener((buttonView, isChecked) -> SharedPreferenceHelper.putShowIndicator(isChecked));
        ((Switch) findViewById(R.id.switchTip)).setOnCheckedChangeListener((buttonView, isChecked) -> SharedPreferenceHelper.putShowTip(isChecked));
        resetTime();
    }

    @Override
    protected void onResume() {
        super.onResume();
        String lastDir = SharedPreferenceHelper.getLastDir();
        String history = TextUtils.isEmpty(lastDir) ? "暂无历史记录" : lastDir;
        ((TextView) findViewById(R.id.history)).setText(history);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE:
                if (resultCode == RESULT_OK) {
                    // Get the Uri of the selected file
                    parseUri(data.getData());
                }
                break;
        }
    }

    private void initLocal() {
        int local = SharedPreferenceHelper.getTime();
        if (local >= 1) {
            TIME_PERIOD = local;
        }
    }

    private void resetTime() {
        SharedPreferenceHelper.putTime(TIME_PERIOD);
        ((TextView) findViewById(R.id.time)).setText(String.format("%ds", TIME_PERIOD));
    }

    private void parseUri(Uri uri) {
        try {
            String path = UriParser.getRealPathFromUri(this, uri);
            if (!TextUtils.isEmpty(path)) {
                PhotoActivity.Open(this, path);
            } else {
                ToastUtils.show("路径解析有误:" + uri.toString());
            }
        } catch (Exception ex) {
            ToastUtils.show(ex.getMessage());
        }
    }

    private void setClick(int viewID, Runnable action) {
        findViewById(viewID).setOnClickListener(v -> action.run());
    }
}